/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.utilitario;

import java.util.ArrayList;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Wenderson
 */
public class TabelaDinamica<T extends AbstractTabelaDinamica> extends AbstractTableModel {

    private ArrayList<T> lista;
    private String[] colunas;
    private T object;
    
    public TabelaDinamica(T object) {
        this.lista = new ArrayList<T>();
        this.object = object;
        this.colunas = object.getColumns();
    }
    
    @Override
    public int getRowCount() {
        return this.lista.size();
    }

    @Override
    public int getColumnCount() {
        return this.colunas.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return this.colunas[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return this.object.getClass(columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return this.object.isEditable(columnIndex);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return this.lista.get(rowIndex).getValue(columnIndex);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        this.lista.get(rowIndex).setValue(columnIndex, aValue);
    }
    
    public boolean exists(T value) {
        for (T objeto : this.lista) {
            if (objeto.equals(value)) {
                return true;
            }
        }
        return false;
    }
    
    public int getSize() {
        return this.lista.size();
    }
    
    public ArrayList<T> getList() {
        return this.lista;
    }
    
    public T getValue(int index) {
        if (index >= 0 && index < this.lista.size()) {
            return this.lista.get(index);
        }
        return null;
    }

    public void inserir(ArrayList<T> lista) {
        this.lista = lista;
        fireTableDataChanged();
    }
    
    public void inserir(T value) {
        this.lista.add(value);
        fireTableDataChanged();
    }
    
    public void remover(int row) {
        this.lista.remove(row);
        fireTableDataChanged();
    }
    
    public void clear() {
        this.lista.clear();
        fireTableDataChanged();
    }
}
